package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.CarPlanWaybilListActivity;
import com.chuanyang.driverh.mainhome.GoodsAssignWeightNumActivity;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.response2.ResultCarPlan;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.List;



public class CarPlanAdapter extends BaseListAdapter<ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity> {//首页竞价大厅的adapter
    private Activity activity;
    public CarPlanAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity=activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder=null;

        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_assign_car, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.line_left = convertView.findViewById(R.id.line_left);
            viewHolder.tv_has_receive = (TextView) convertView.findViewById(R.id.tv_has_receive);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            viewHolder.tv_unit = (TextView) convertView.findViewById(R.id.tv_unit);
            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_price_unit = (TextView) convertView.findViewById(R.id.tv_price_unit);
            viewHolder.tv_time_load = (TextView) convertView.findViewById(R.id.tv_time_load);
            viewHolder.tv_time_unload= (TextView) convertView.findViewById(R.id.tv_time_unload);
            viewHolder.tv_order_type= (TextView) convertView.findViewById(R.id.tv_order_type);
            viewHolder.tv_see_assign= (TextView) convertView.findViewById(R.id.tv_see_assign);
            viewHolder.tv_assign= (TextView) convertView.findViewById(R.id.tv_assign);
            viewHolder.tv_cancel_menmber= (TextView) convertView.findViewById(R.id.tv_cancel_menmber);
            viewHolder.tv_notice_no= (TextView) convertView.findViewById(R.id.tv_notice_no);
            viewHolder.tv_load= (TextView) convertView.findViewById(R.id.tv_load);
            viewHolder.tv_unload= (TextView) convertView.findViewById(R.id.tv_unload);
            viewHolder.ll_div= (LinearLayout) convertView.findViewById(R.id.ll_div);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
       final ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity  data= getItem(position);

        if (StringUtil.isBlank(data.getNoticeNo())){
            viewHolder.tv_notice_no.setVisibility(View.GONE);
        }else {
            viewHolder.tv_notice_no.setVisibility(View.VISIBLE);
            if (data.getNoticeNo().contains(",")){
                viewHolder.tv_notice_no.setText(data.getNoticeNo().substring(0,data.getNoticeNo().indexOf(",")-1)+"...");
            }else {
                viewHolder.tv_notice_no.setText(data.getNoticeNo());
            }
        }



//        viewHolder.tv_time.setText(data.getExpiredTime());
        if ("1".equals(data.getShowType())){//待派单
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_gray));
            viewHolder.tv_has_receive.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));
            viewHolder.tv_has_receive.setText("待派单");
            viewHolder.tv_see_assign.setVisibility(View.GONE);
            viewHolder.tv_assign.setVisibility(View.VISIBLE);
            viewHolder.tv_see_assign.setVisibility(View.GONE);

        }else if("2".equals(data.getShowType())){//已派单
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_green));
            viewHolder.tv_has_receive.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));
            viewHolder.tv_has_receive.setText("已派单");
            viewHolder.tv_see_assign.setVisibility(View.VISIBLE);
            viewHolder.tv_assign.setVisibility(View.GONE);
            viewHolder.tv_see_assign.setVisibility(View.VISIBLE);

        }else{//派单中
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_blue));
            viewHolder.tv_has_receive.setTextColor(activity.getResources().getColor(R.color.text_assign_car_blue));
            viewHolder.tv_has_receive.setText("派单中");
            viewHolder.tv_see_assign.setVisibility(View.VISIBLE);
            viewHolder.tv_assign.setVisibility(View.VISIBLE);
            viewHolder.tv_see_assign.setVisibility(View.VISIBLE);
        }
        viewHolder.tv_order_no.setText(data.getOrderNo());

        viewHolder.tv_order_time.setText(data.getOrderTime());
        viewHolder.tv_city_start.setText(data.getBeginCity());
        viewHolder.tv_city_end.setText(data.getEndCity());
//        setGoodName(data.getGoodss());
        setGoodName(viewHolder.tv_goods_name, data.getGoodss());//货品名称

        viewHolder.tv_num.setText(data.getSumGoodsNum()+"");
//        viewHolder.tv_weight.setText(data.getSumGoodsWeight()+"");
        viewHolder.tv_weight.setText(StringUtil.getDouble(data.getSumGoodsWeight()+""));

//        viewHolder.tv_price.setText(StringUtil.getDouble(data.getPrice()+""));


       /* viewHolder.tv_time_load.setText(data.getStartTime());
        if (!StringUtil.isBlank(data.getEndTime())) {
            viewHolder.tv_time_unload.setText(data.getEndTime());
        }else {
            viewHolder.tv_time_unload.setText("不限");
        }*/



        if (data.getTransPortWay() == 2)//船
        {
            viewHolder.tv_load.setText("装卸天数");
            viewHolder.tv_unload.setText("受载日期");
            viewHolder.tv_load.setPadding(6,0,6,0);
            viewHolder.tv_unload.setPadding(6,0,6,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_shape_round_corner_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_shape_round_corner_unload);

            viewHolder.tv_time_load.setText(data.getStevedoreDay()+"天");
            viewHolder.tv_time_unload.setText(data.getStartTime()+"±"+data.getFq()+"天");
        }else {
            viewHolder.tv_load.setText("提");
            viewHolder.tv_unload.setText("卸");
            viewHolder.tv_load.setPadding(0,0,0,0);
            viewHolder.tv_unload.setPadding(0,0,0,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_point_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_point_unload);
            viewHolder.tv_time_load.setText(data.getStartTime());
            if (!StringUtil.isBlank(data.getEndTime())) {
                viewHolder.tv_time_unload.setText(data.getEndTime());
            }else {
                viewHolder.tv_time_unload.setText("不限");
            }

        }











        viewHolder.tv_order_type.setText("");
        if (data.getOrderType()==1){//1竞价
            viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_t);
        }else if(data.getOrderType()==2){//匹配
            viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_t);
        }else{//专线
            viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_t);
        }
//        viewHolder.tv_unit.setBackgroundColor(data.getUnit());

        viewHolder.tv_see_assign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.startActivity(new Intent(activity, CarPlanWaybilListActivity.class).putExtra("data",data.getOrderId()+""));
            }
        });
        viewHolder.tv_assign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                activity.startActivity(new Intent(activity, GoodsAssignActivity.class).putExtra("data",data.getOrderId()+""));
                activity.startActivity(new Intent(activity, GoodsAssignWeightNumActivity.class).putExtra("data",data.getOrderId()+""));
//                activity.startActivity(new Intent(activity, AssignGoodsActivity.class));
            }
        });
        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(activity, OrderDetailRceiveActivity.class);
                intent.putExtra("data",data.getOrderId()+"");
                if ("1".equals(data.getShowType())){
                    intent.putExtra("type","5");
                }else if ("2".equals(data.getShowType())){
                    intent.putExtra("type","7");
                }else if ("3".equals(data.getShowType())){
                    intent.putExtra("type","6");
                }else {
                    intent.putExtra("type","0");
                }
                activity.startActivity(intent);
            }
        });

        if (data.getCurrency()==2){
            viewHolder.tv_price_unit.setText("$/t");
        }else {
            viewHolder.tv_price_unit.setText("¥/t");
        }
        viewHolder.tv_price.setText( StringUtil.getDouble(data.getPrice()));


  /*      if (data.getReturned()!=0){
            viewHolder.tv_cancel_menmber.setText(data.getReturned()+"");
            viewHolder.ll_cancel_menmber.setVisibility(View.VISIBLE);
        }else{
            viewHolder.ll_cancel_menmber.setVisibility(View.GONE);
        }*/



        if (!StringUtil.isBlank(data.getIsCede()) && data.getIsCede().contains("1")){
//            viewHolder.tv_cancel_menmber.setText(data.getReturned()+"");

            viewHolder.tv_cancel_menmber.setVisibility(View.VISIBLE);
        }else{
            viewHolder.tv_cancel_menmber.setVisibility(View.GONE);
        }

        return convertView;
    }

    private void setGoodName(TextView view, List<ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity.GoodssEntity> goodss) {
        StringBuilder	builder = new StringBuilder();
        for (ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity.GoodssEntity info: goodss){
            builder.append(info.getGoodsName() + ",");
        }
        String temp=builder.toString();
        String result=temp.substring(0,temp.length()-1);
        view.setText(result);
    }

    class ViewHolder {
        View line_left;
        TextView tv_has_receive;
        TextView tv_order_no;
        TextView tv_order_time;
        TextView tv_city_start;
        TextView tv_city_end;
        TextView tv_goods_name;
        TextView tv_num;
        TextView tv_unit;
        TextView tv_weight;
        TextView tv_price;
        TextView tv_price_unit;
        TextView tv_time_load;
        TextView tv_time_unload;
        TextView tv_order_type;
        TextView tv_see_assign;
        TextView tv_assign;
        TextView tv_cancel_menmber;
        TextView tv_notice_no;
        TextView tv_load;
        TextView tv_unload;
        LinearLayout ll_div;
    }
}
